package com.lty.bbapp.custom;

import java.util.Collections;
import java.util.List;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.lty.entity.Bus;

/**
 * @Title: DataHelper.java
 * @Package com.lty.custom
 * @Description:
 * @author yangyang
 * @date 2017年3月31日 下午6:29:17
 * @version v1.0
 */
public class DataHelper {
	private static String url;

	private static List<Bus> buses;

	private DataHelper() {
	}

	public synchronized static List<Bus> getAllBuses() {
		if (CollectionUtils.isEmpty(buses)) {
			String result = HttpClientHelper.get(url, null);
			
			if (StringUtils.isEmpty(result) || result.startsWith("error")) {
				return Collections.emptyList();
			}
			
			JSONObject jsonObject = JSONObject.parseObject(result);
			if (!jsonObject.getBooleanValue("success")) {
				return Collections.emptyList();
			}
			result = jsonObject.getString("obj");
			buses = JSONObject.parseArray(result, Bus.class);
		}
		return buses;
	}

	public static String getUrl() {
		return url;
	}

	public static void setUrl(String url) {
		DataHelper.url = url;
	}

}
